***********************
Version history : 
Change 2022/10/25
Departure only starting in T-1
;
option nocenter nodate nonumber ps=max ls=max; 

dm 'clear log';
dm 'clear output';


proc format; 
value $naf03_naf08_
"011A"="0111Z"
"011C"="0111Z"
"011D"="0113Z"
"011F"="0122Z"
"011G"="0121Z"
"012A"="0141Z"
"012C"="0143Z"
"012E"="0146Z"
"012G"="0147Z"
"012J"="0144Z"
"013Z"="0150Z"
"014A"="0161Z"
"014B"="8130Z"
"014D"="0162Z"
"015Z"="0170Z"
"020A"="0129Z"
"020B"="0220Z"
"020D"="0240Z"
"050A"="0311Z"
"050C"="0321Z"
"101Z"="0510Z"
"102Z"="0520Z"
"103Z"="0892Z"
"111Z"="0610Z"
"112Z"="0910Z"
"120Z"="0721Z"
"131Z"="0710Z"
"132Z"="0729Z"
"141A"="0811Z"
"141C"="0811Z"
"141E"="0811Z"
"142A"="0812Z"
"142C"="0812Z"
"143Z"="0891Z"
"144Z"="0893Z"
"145Z"="0899Z"
"151A"="0149Z"
"151C"="1011Z"
"151E"="1013A"
"151F"="1013B"
"152Z"="1020Z"
"153A"="1031Z"
"153C"="1032Z"
"153E"="1039A"
"153F"="1039B"
"154A"="1041A"
"154C"="1041B"
"154E"="1042Z"
"155A"="1051A"
"155B"="1051B"
"155C"="1051C"
"155D"="1051D"
"155F"="1052Z"
"156A"="1061A"
"156B"="1061B"
"156D"="1062Z"
"157A"="1091Z"
"157C"="1092Z"
"158A"="1071A"
"158B"="1071B"
"158C"="1071C"
"158D"="1071D"
"158F"="1072Z"
"158H"="1081Z"
"158K"="1082Z"
"158M"="1073Z"
"158P"="1083Z"
"158R"="1084Z"
"158T"="1086Z"
"158V"="1085Z"
"159A"="1101Z"
"159B"="1101Z"
"159D"="1101Z"
"159F"="1102A"
"159G"="1102B"
"159J"="1103Z"
"159L"="1104Z"
"159N"="1105Z"
"159Q"="1106Z"
"159S"="1107A"
"159T"="1107B"
"160Z"="1200Z"
"171A"="1310Z"
"171C"="1310Z"
"171E"="1310Z"
"171F"="1310Z"
"171H"="1310Z"
"171K"="1310Z"
"171M"="1310Z"
"171P"="1310Z"
"172A"="1320Z"
"172C"="1320Z"
"172E"="1320Z"
"172G"="1320Z"
"172J"="1320Z"
"173Z"="1330Z"
"174A"="1392Z"
"174B"="1392Z"
"174C"="1392Z"
"175A"="1393Z"
"175C"="1394Z"
"175E"="1395Z"
"175G"="1396Z"
"176Z"="1391Z"
"177A"="1419Z"
"177C"="1439Z"
"181Z"="1411Z"
"182A"="1412Z"
"182C"="1413Z"
"182D"="1413Z"
"182E"="1413Z"
"182G"="1414Z"
"182J"="1419Z"
"183Z"="1320Z"
"191Z"="1511Z"
"192Z"="1512Z"
"193Z"="1520Z"
"201A"="1610A"
"201B"="1610B"
"202Z"="1621Z"
"203Z"="1622Z"
"204Z"="1624Z"
"205A"="1629Z"
"205C"="1629Z"
"211A"="1711Z"
"211C"="1712Z"
"212A"="1721A"
"212B"="1721B"
"212C"="1721C"
"212E"="1722Z"
"212G"="1723Z"
"212J"="1724Z"
"212L"="1729Z"
"221A"="3299Z"
"221C"="5813Z"
"221E"="5814Z"
"221G"="5920Z"
"221J"="5819Z"
"222A"="1811Z"
"222C"="1723Z"
"222E"="1814Z"
"222G"="1813Z"
"222J"="1813Z"
"223A"="1820Z"
"223C"="1820Z"
"223E"="1820Z"
"231Z"="1910Z"
"232Z"="1920Z"
"233Z"="2013A"
"241A"="2011Z"
"241C"="2012Z"
"241E"="2013B"
"241G"="1910Z"
"241J"="0891Z"
"241L"="2016Z"
"241N"="2017Z"
"242Z"="2020Z"
"243Z"="2030Z"
"244A"="2110Z"
"244C"="2120Z"
"244D"="2120Z"
"245A"="2041Z"
"245C"="2042Z"
"246A"="2051Z"
"246C"="2052Z"
"246E"="2053Z"
"246G"="2059Z"
"246J"="2680Z"
"246L"="2059Z"
"247Z"="2060Z"
"251A"="2211Z"
"251C"="2211Z"
"251E"="2219Z"
"252A"="2221Z"
"252C"="2222Z"
"252E"="2223Z"
"252G"="2229A"
"252H"="2229A"
"261A"="2311Z"
"261C"="2312Z"
"261E"="2313Z"
"261G"="2314Z"
"261J"="2319Z"
"261K"="2319Z"
"262A"="2341Z"
"262C"="2342Z"
"262E"="2343Z"
"262G"="2344Z"
"262J"="2349Z"
"262L"="2320Z"
"263Z"="2331Z"
"264A"="2332Z"
"264B"="2332Z"
"264C"="2332Z"
"265A"="2351Z"
"265C"="2352Z"
"265E"="2352Z"
"266A"="2361Z"
"266C"="2362Z"
"266E"="2363Z"
"266G"="2364Z"
"266J"="2365Z"
"266L"="2369Z"
"267Z"="2370Z"
"268A"="2391Z"
"268C"="2399Z"
"271Y"="2410Z"
"272A"="2451Z"
"272C"="2420Z"
"273A"="2431Z"
"273C"="2432Z"
"273E"="2433Z"
"273G"="2434Z"
"274A"="2441Z"
"274C"="2442Z"
"274D"="2442Z"
"274F"="2443Z"
"274G"="2443Z"
"274J"="2444Z"
"274K"="2444Z"
"274M"="2445Z"
"275A"="2451Z"
"275C"="2452Z"
"275E"="2453Z"
"275G"="2454Z"
"281A"="2433Z"
"281C"="2512Z"
"282C"="2529Z"
"282D"="2521Z"
"283A"="2530Z"
"283B"="2530Z"
"283C"="3311Z"
"284A"="2550A"
"284B"="2550B"
"284C"="2550A"
"285A"="2561Z"
"285C"="2562A"
"285D"="2562B"
"286A"="2571Z"
"286C"="2573B"
"286D"="2573B"
"286F"="2572Z"
"287A"="2591Z"
"287C"="2592Z"
"287E"="2593Z"
"287G"="2594Z"
"287H"="2593Z"
"287J"="2593Z"
"287L"="2599A"
"287N"="2599B"
"287Q"="2571Z"
"291A"="2811Z"
"291B"="2812Z"
"291D"="2812Z"
"291E"="2813Z"
"291F"="2812Z"
"291H"="2815Z"
"291J"="2815Z"
"292A"="2821Z"
"292C"="2822Z"
"292D"="2822Z"
"292F"="2825Z"
"292H"="2829A"
"292J"="2829A"
"292L"="2829B"
"292M"="2829B"
"293A"="2830Z"
"293C"="3312Z"
"293D"="2830Z"
"294A"="2841Z"
"294B"="2849Z"
"294C"="2824Z"
"294D"="2790Z"
"294E"="2849Z"
"295A"="2891Z"
"295B"="2892Z"
"295D"="2892Z"
"295E"="2893Z"
"295G"="2894Z"
"295J"="2895Z"
"295L"="2894Z"
"295M"="2896Z"
"295N"="2573A"
"295Q"="2899B"
"295R"="2894Z"
"296A"="2540Z"
"296B"="2540Z"
"297A"="2751Z"
"297C"="2752Z"
"300A"="2620Z"
"300C"="2620Z"
"311A"="2611Z"
"311B"="2611Z"
"311C"="3313Z"
"312A"="2611Z"
"312B"="2611Z"
"313Z"="2611Z"
"314Z"="2720Z"
"315A"="2740Z"
"315B"="2740Z"
"315C"="2740Z"
"316A"="2740Z"
"316C"="2599B"
"316D"="2344Z"
"321A"="2611Z"
"321C"="2611Z"
"321D"="2612Z"
"322A"="2630Z"
"322B"="2630Z"
"323Z"="2620Z"
"331A"="2660Z"
"331B"="2660Z"
"332A"="2651A"
"332B"="2651B"
"333Z"="3320C"
"334A"="3250B"
"334B"="2670Z"
"335Z"="2652Z"
"341Z"="2892Z"
"342A"="2920Z"
"342B"="2920Z"
"343Z"="2811Z"
"351A"="3011Z"
"351B"="3011Z"
"351C"="3011Z"
"351E"="3012Z"
"352Z"="3020Z"
"353A"="2899B"
"353B"="3030Z"
"353C"="3030Z"
"354A"="3091Z"
"354C"="3092Z"
"354E"="3092Z"
"355Z"="2822Z"
"361A"="2932Z"
"361C"="2823Z"
"361E"="3102Z"
"361G"="3109B"
"361H"="3109B"
"361J"="3109B"
"361K"="3109B"
"361M"="3103Z"
"362A"="3211Z"
"362C"="3212Z"
"363Z"="3220Z"
"364Z"="3230Z"
"365Z"="2640Z"
"366A"="3213Z"
"366C"="3291Z"
"366E"="1399Z"
"371Z"="3831Z"
"372Z"="3831Z"
"401A"="3511Z"
"401C"="3512Z"
"401E"="3513Z"
"402A"="3521Z"
"402C"="3522Z"
"403Z"="3530Z"
"410Z"="3600Z"
"451A"="4311Z"
"451B"="4312B"
"451D"="4313Z"
"452A"="4120A"
"452B"="4120B"
"452C"="4120B"
"452D"="4212Z"
"452E"="4221Z"
"452F"="4222Z"
"452J"="4391B"
"452K"="4399A"
"452L"="4391A"
"452N"="4212Z"
"452P"="4120B"
"452R"="4221Z"
"452T"="4399B"
"452U"="3900Z"
"452V"="4399C"
"453A"="4321A"
"453C"="4329A"
"453E"="4322A"
"453F"="4322B"
"453H"="4321B"
"454A"="4331Z"
"454C"="4332A"
"454D"="4332B"
"454F"="4333Z"
"454H"="4334Z"
"454J"="4334Z"
"454L"="4332C"
"454M"="4339Z"
"455Z"="4399E"
"501Z"="4511Z"
"502Z"="4520A"
"503A"="4531Z"
"503B"="4532Z"
"504Z"="4540Z"
"505Z"="4730Z"
"511A"="4611Z"
"511C"="4612B"
"511E"="4613Z"
"511G"="4614Z"
"511J"="4615Z"
"511L"="4616Z"
"511N"="4617B"
"511P"="4617A"
"511R"="4618Z"
"511T"="4619B"
"511U"="4612A"
"512A"="4621Z"
"512C"="4622Z"
"512E"="4623Z"
"512G"="4624Z"
"512J"="4621Z"
"513A"="1039A"
"513C"="4632A"
"513D"="4632B"
"513E"="4632C"
"513G"="4633Z"
"513J"="1101Z"
"513L"="4635Z"
"513N"="4636Z"
"513Q"="4637Z"
"513S"="4638A"
"513T"="4638B"
"513V"="4639A"
"513W"="4639B"
"514A"="4641Z"
"514C"="4642Z"
"514D"="4642Z"
"514F"="4643Z"
"514H"="4644Z"
"514J"="4644Z"
"514L"="4645Z"
"514N"="4646Z"
"514Q"="4649Z"
"514R"="4649Z"
"514S"="4643Z"
"515A"="4671Z"
"515C"="4672Z"
"515E"="4673A"
"515F"="4673A"
"515H"="4674A"
"515J"="4674B"
"515L"="4675Z"
"515N"="4676Z"
"515Q"="4677Z"
"518A"="4662Z"
"518C"="4663Z"
"518E"="4664Z"
"518G"="4651Z"
"518H"="4665Z"
"518J"="4652Z"
"518L"="4669A"
"518M"="4669B"
"518N"="4669C"
"518P"="4661Z"
"519A"="4690Z"
"519B"="4690Z"
"521A"="4711A"
"521B"="4711B"
"521C"="4711C"
"521D"="4711D"
"521E"="4711E"
"521F"="4711F"
"521H"="4719A"
"521J"="4719B"
"522A"="4721Z"
"522C"="4722Z"
"522E"="4723Z"
"522G"="4724Z"
"522J"="4725Z"
"522L"="4726Z"
"522N"="4729Z"
"522P"="4721Z"
"523A"="4773Z"
"523C"="4774Z"
"523E"="4775Z"
"524A"="4751Z"
"524C"="4771Z"
"524E"="4772A"
"524F"="4772B"
"524H"="4759A"
"524J"="4753Z"
"524L"="4743Z"
"524N"="4752A"
"524P"="4752B"
"524R"="4761Z"
"524T"="4778A"
"524U"="4753Z"
"524V"="4777Z"
"524W"="4764Z"
"524X"="4776Z"
"524Y"="4778B"
"524Z"="4741Z"
"525Z"="4779Z"
"526A"="4791A"
"526B"="4791B"
"526D"="4781Z"
"526E"="4782Z"
"526G"="4799A"
"526H"="4799B"
"527A"="9523Z"
"527C"="9521Z"
"527D"="9522Z"
"527F"="9525Z"
"527H"="9512Z"
"551A"="5510Z"
"551C"="5510Z"
"551E"="5510Z"
"552A"="5520Z"
"552C"="5530Z"
"552E"="5510Z"
"552F"="5590Z"
"553A"="5610A"
"553B"="5610C"
"554A"="5630Z"
"554B"="5630Z"
"554C"="5630Z"
"555A"="5629B"
"555C"="5629A"
"555D"="5621Z"
"601Z"="4910Z"
"602A"="4931Z"
"602B"="4939A"
"602C"="4931Z"
"602E"="4932Z"
"602G"="4939B"
"602L"="4941B"
"602M"="4941A"
"602N"="4942Z"
"602P"="4941C"
"603Z"="4950Z"
"611A"="5010Z"
"611B"="5010Z"
"612Z"="5030Z"
"621Z"="5110Z"
"622Z"="5110Z"
"623Z"="5122Z"
"631A"="5224A"
"631B"="5224B"
"631D"="5210A"
"631E"="5210B"
"632A"="4932Z"
"632C"="5222Z"
"632E"="5223Z"
"633Z"="7911Z"
"634A"="5229A"
"634B"="5229B"
"634C"="5229B"
"641A"="5310Z"
"641C"="5320Z"
"642C"="6110Z"
"642D"="6110Z"
"651A"="6411Z"
"651C"="6419Z"
"651D"="6419Z"
"651E"="6419Z"
"651F"="6419Z"
"652A"="6491Z"
"652C"="6492Z"
"652E"="6420Z"
"652F"="6499Z"
"660A"="6511Z"
"660C"="6520Z"
"660E"="6512Z"
"660F"="6520Z"
"660G"="6512Z"
"671A"="6611Z"
"671C"="6612Z"
"671E"="6612Z"
"672Z"="6621Z"
"701A"="4110A"
"701B"="4110B"
"701C"="4110C"
"701D"="4110D"
"701F"="6810Z"
"702A"="6820A"
"702B"="6820B"
"702C"="6820B"
"703A"="6831Z"
"703C"="6832A"
"703D"="6832A"
"703E"="6619A"
"711A"="7711A"
"711B"="7711B"
"712A"="7712Z"
"712C"="7734Z"
"712E"="7735Z"
"713A"="7731Z"
"713C"="7732Z"
"713E"="7733Z"
"713G"="7739Z"
"714A"="7729Z"
"714B"="7721Z"
"721Z"="6202A"
"722A"="5821Z"
"722C"="6201Z"
"723Z"="6203Z"
"724Z"="5811Z"
"725Z"="3312Z"
"726Z"="6209Z"
"731Z"="7211Z"
"732Z"="7220Z"
"741A"="6910Z"
"741C"="6920Z"
"741E"="7320Z"
"741G"="0240Z"
"741J"="6420Z"
"742A"="7111Z"
"742B"="7112A"
"742C"="7112B"
"743A"="7120A"
"743B"="7120B"
"744A"="7311Z"
"744B"="7311Z"
"745A"="7810Z"
"745B"="7820Z"
"746Z"="7490B"
"747Z"="8121Z"
"748A"="7420Z"
"748B"="7420Z"
"748D"="8292Z"
"748F"="7430Z"
"748G"="8219Z"
"748H"="8220Z"
"748J"="8230Z"
"748K"="4779Z"
"751A"="8411Z"
"751C"="8412Z"
"751E"="8413Z"
"751G"="8110Z"
"752A"="8421Z"
"752C"="8422Z"
"752E"="8423Z"
"752G"="8424Z"
"752J"="8425Z"
"753A"="8430A"
"753B"="8430B"
"753C"="8430C"
"801Z"="8510Z"
"802A"="8531Z"
"802C"="8532Z"
"803Z"="8541Z"
"804A"="8532Z"
"804C"="8532Z"
"804D"="8552Z"
"851A"="8610Z"
"851C"="8621Z"
"851E"="8623Z"
"851G"="8690D"
"851H"="8690E"
"851J"="8690A"
"851K"="8690B"
"851L"="8690C"
"852Z"="7500Z"
"853A"="8710B"
"853B"="8710B"
"853C"="8710C"
"853D"="8710A"
"853E"="8720A"
"853G"="8891A"
"853H"="8810B"
"853J"="8810A"
"853K"="8899A"
"900A"="3700Z"
"900B"="3811Z"
"900E"="3812Z"
"900G"="3811Z"
"911A"="9411Z"
"911C"="9412Z"
"912Z"="9420Z"
"913A"="9491Z"
"913C"="9492Z"
"913E"="9499Z"
"921A"="5911A"
"921B"="5911B"
"921C"="5911C"
"921D"="5911C"
"921F"="5913A"
"921G"="5913B"
"921J"="5914Z"
"922A"="5920Z"
"922B"="5911A"
"922D"="6020A"
"922E"="6020B"
"922F"="6110Z"
"923A"="9001Z"
"923B"="9002Z"
"923D"="7990Z"
"923F"="9321Z"
"923K"="7990Z"
"924Z"="6391Z"
"925A"="9101Z"
"925C"="9102Z"
"925E"="9104Z"
"926A"="9311Z"
"926C"="7990Z"
"927A"="9200Z"
"927C"="0162Z"
"930A"="9601A"
"930B"="9601B"
"930D"="9602A"
"930E"="9602B"
"930G"="9603Z"
"930H"="9603Z"
"930K"="9604Z"
"930L"="9313Z"
"930N"="8551Z"
"950Z"="9700Z"
"960Z"="9810Z"
"970Z"="9820Z"
"990Z"="9900Z"
"000Z"=""
"271Z"="2410Z"
"291C"="2812Z"
"295P"="2573A"
"321B"="2611Z"
"516A"="4671Z"
"516C"="4672Z"
"516G"="4676Z"
"516J"="4676Z"
"516K"="4676Z"
"516L"="4676Z"
"516N"="4676Z"
"517Z"="4676Z"
"642B"="6110Z"
"711Z"="7711B"
"722Z"="6209Z"
"7801"=""
"922C"="5911A"
"923H"="7990Z"
;
run;


* CHANGING THE WORK DIRECTORY; 
libname user "C:\Users\Public\Documents\Olivier\Sas_work_dir_2";
libname rec "C:\Users\Public\Documents\Results\Worker_Flows";

libname lifi19 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2019";
libname lifi18 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2018";
libname lifi17 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2017";
libname lifi16 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2016";
libname lifi15 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2015";
libname lifi14 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2014";
libname lifi13 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2013";
libname lifi12 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_LIFI_2012";
libname lifi11 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2011";	
libname lifi10 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2010";
libname lifi09 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2009";
libname lifi08 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2008";
libname lifi07 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2007";
libname lifi06 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2006";
libname lifi05 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2005";
libname lifi04 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2004";
libname lifi03 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2003";
libname lifi02 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2002";
libname lifi02 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2002";
libname lifi01 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2001";
libname lifi00 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_2000";
libname lifi99 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1999";
libname lifi98 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1998";
libname lifi97 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1997";
libname lifi96 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1996";
libname lifi95 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1995";
libname lifi94 "\\casd.fr\casdfs\Projets\INEPROG\Data\LIFI_Enqu�te LIFI_1994";

*to be completed;

*libname test "P:\Test_data";
libname po1994 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1994";
libname po1995 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1995";
libname po1996 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1996";
libname po1997 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1997";
libname po1998 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1998";
libname po1999 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1999";
libname po2000 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2000";
libname po2001 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2001";
libname po2002 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2002";
libname po2003 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2003";
libname po2004 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2004";
libname po2005 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2005";
libname po2006 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2006";
libname po2007 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2007";
libname po2008 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2008";
libname po2009 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2009";
libname po2010 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2010";
libname po2011 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2011";
libname po2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012";
libname po2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013";
libname po2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\R�gions";
libname po2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\R�gions";
libname po2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\R�gions";
libname po2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\R�gions";
libname po2018 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2018";
libname po2019 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2019";

libname pi2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012\Ile de France";
libname pi2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013\Ile de France";
libname pi2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\D�partements";
libname pi2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\D�partements";
libname pi2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\D�partements";
libname pi2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\D�partements";


proc format; 
value tmob
5.1="51_collective move_Interim"
5="5_collective move"
4="4_chg of business group"
3.3="33_Out/back sourcing in Interim"
3.2="32_Interim outsourcing"
3.1="31_Interim backsourcing"
3="3_Outsourcing / backsourcing"
2="2_chg firm but not business group"
1="1_chg workplace but not firm"
0="0_immobile";

value dmob
2="2_mobility between -1 +3 months after termination"
3="3_mobility more than 3 months after termination"
1="1_mobility more than 1 month before termination (overlap)"
0="0_immobile";

value nb
	0="0"
	1="1"
	2-5="2-5"
	6-10="6-10"
	11-20="11-20"
	21-50="21-50"
	51-100="51-100"
	101-200="101-200"
	201-500="201-500"
	501-1000000000000="501+"
	;

value type_m
	1="stayers"
	2="potential movers"
	3="excluded"
	;

run;



%macro out(b);
	%if &b=b1994_1 %then %let year=1993.1;
	%else %if &b=b2002_1 %then %let year=2001.1;
	%else %if &b=b2009_1 %then %let year=2008.1;
	%else %let year=%substr(&b,2,4);

	%let psyear=&year;
	%if &year<1995 %then %let psyear=1995;
	%if &year=2001.1 %then %let psyear=2002;
	%if &year=2008.1 %then %let psyear=2009;

	%let year2=%substr(&psyear,3,2);

* 1. On prend uniquement les mobiles qui soit �taient dans 1 entreprise 
en end de p�riode et dans une autre en d�but de p�riode; 
* Exclusion des petites mobilit�s; 
* UNIQUEMENT LES POSTES A TEMPS COMPLET / DISCUTABLE;

proc sql; 
		Create Table NBsiret_1 as select min(SIRET) as SIRET, 
				count(SIRET) AS NBSIRET_1 FROM &b (KEEP=SIRET DATDEB_1 WHERE=(DATDEB_1<=1) ) group by SIRET;

		Create Table NBsiret as select min(SIRET) as SIRET, 
				count(SIRET) AS NBSIRET FROM &b (KEEP=SIRET DATFIN WHERE=(DATFIN>=360)) group by SIRET;
	quit;


	data nm bm tm; 
		set &b (keep= SIRET SIREN IDENT_S DATDEB DATFIN DATDEB_1 
							DATFIN_1 CPFD CPFD_1 APET_1 APET YEAR);
		if datdeb_1=. then start=DATDEB+360;
		else start=DATDEB_1;

		if DATFIN=. then end=DATFIN_1;
		else end=DATFIN+360;

		drop DATDEB_1 DATFIN DATFIN_1 CPFD CPFD_1 /*EFF_MOY_ET EFF_MOY_ET_1*/ ; 

		duration=end-start;

		if start<=1 and CPFD_1="C" and  CPFD="C"  and end>=720 then output nm;
		else if start<=1 and end>=720 then output tm;
		else if (start<=1 and CPFD_1="C") or (CPFD="C" and end>=720) then output bm;
		else output tm;
	run;

	proc sql; 
		create table bm0 as select *, min(start) as mindeb, max(end) as maxfin, count(IDENT_S) as nbjob from bm group by IDENT_S; 
		%if &year<2014 %then %do; 
			create table bm1a(drop=sirlifi) as select * from bm as aa left join 
					lifi&year2..ent (keep=sirlifi sirtg RS) 
					as bb on aa.siren=bb.sirlifi;
		%end;
		%else %do; 
			create table bm1a(drop=sirlifi) as select * from bm as aa left join 
					lifi&year2..ul_lifi (keep=id_ul id_groupe RAISON_SOCIALE rename=(id_ul=sirlifi id_groupe=sirtg 
										RAISON_SOCIALE=RS)) 
					as bb on aa.siren=bb.sirlifi;
		%end;

		create table bm1b as select * from bm1a as aa left join nbsiret_1 as bb on aa.siret=bb.siret;
		create table bm1 as select * from bm1b as aa left join nbsiret as bb on aa.siret=bb.siret;
		
	quit;

	proc datasets lib=user;
	 delete NBsiret_1 NBsiret bm1a bm1b; 
	run; 

	
	*ON trie par la dur�e;
	proc sort data=bm1; 
		by IDENT_S start DESCENDING duration;
	run;

	*We drop simultaneous jobs ;
	data bm2 bm1_drop; 
		set bm1; 
		if ident_s=lag(ident_s) and start<=1 and lag(start)<=1 then output bm1_drop;
		else if ident_s=lag(ident_s) and end>=720 and lag(end)>=720 then output bm1_drop;
		else output bm2;
	run;
	
	proc datasets lib=user;
	 delete bm1; 
	run; 

	proc sql; 
		create table bm2_bis as select *, count(ident_s) as nb_ident from bm2 group by IDENT_s; 
	quit;

	proc freq; 
		table nb_ident;
	run;

	proc datasets lib=user;
	 delete bm2; 
	run; 

	proc sort data=bm2_bis; 
		by IDENT_S start DESCENDING duration;
	run;

	*On classe les types de mobilit� et on ne garde que les mobilit�s; 
	data bm3 bm3_bis bm3_ter; set bm2_bis ; 
		lstart=lag(start);
		lend=lag(end);
		lsiren=lag(siren);
		lsiret=lag(siret);
		lrs=lag(rs);
		lsirtg=lag(sirtg);
		if nbsiret=. then nbsiret=0;
		lnbsiret=lag(nbsiret);
		if nbsiret_1=. then nbsiret_1=0;
		lnbsiret_1=lag(nbsiret_1);
		lapet_1=lag(apet_1);

		if year<2008 then do ;
			lind_1=put(lapet_1,$naf03_naf08_.);
			ind=put(apet,$naf03_naf08_.);
		end;
		else do; 
			lind_1=lapet_1;
			ind=apet;

		end;

		if ident_s=lag(ident_S) then do; 
			trajet=COMPBL(LSIRET !! "#" !! SIRET );
			ecart=start-lend;
			if (sirtg NE lsirtg and lsirtg NE "" and sirtg NE "") or
				(siren NE lsiren AND (SIRTG="" or Lsirtg=""))
			then tmob=4;
			else if sirtg=lsirtg and siren NE lsiren then tmob=2;
			else tmob=1;
			if 92 >= ecart >= -31 and end>lend THEN  dmob=2;
			else if ecart > 92 then dmob=3;
			else if ecart < -31 then dmob=1;
		end;
		else tmob=0;

		if nb_ident=1 then output bm3_bis;
		else if (nb_ident=2 and tmob>0 and lend>360)  then output bm3_ter;
		else if nb_ident=2 and tmob>0 then output bm3;
	run;

	proc datasets lib=user;
	 	delete bm2_BIS; 
	run; 



	* On trie par mobilit�, pour s�parer les mobilit�s sur le coeur de cible; 
	proc sort data=bm3; 
		by trajet dmob lend; 
	run;

	*On consid�re que la mobilit� est group�e si la personne qui suit le m�me parcours part 
	moins de 4 mois apr�s son pr�d�cesseur;

	* A REVOIR SANS DMOB; 
	data bm4; set bm3; 
	l2lend=lag(lend);
	*ldmob=lag(dmob);
	retain idtraj 1 /*idtraj2 1*/ rgidtraj 1 /*rgidtraj2 1 debmobco 0*/;
	if trajet NE lag(trajet) then do;
		idtraj=idtraj+1;
		rgidtraj=1;
	end;
	else do; 
		rgidtraj=rgidtraj+1;
		if lend>l2lend+123 then do;
			idtraj=idtraj+1;
			rgidtraj=1;
		end;
	end;
	drop l2lend /*ldmob debmobco*/;
	run;

	proc datasets lib=user;
	 	delete bm3; 
	run; 


	proc sql; 
		create table bm5 as select *, count(idtraj) as nbmob from bm4 
		group by idtraj; 
	quit;

	proc datasets lib=user;
	 	delete bm4; 
	run; 


	data bm6 (drop=RS LRS); set bm5; 
		
		txmobco=nbmob/min(nbsiret,lnbsiret_1);
		txmobco2=nbmob/max(nbsiret,lnbsiret_1);

		if nbmob>2 and tmob=4 and txmobco<0.5 and nbmob<10 then tmob=5;
		else if nbmob>5 and tmob=4 and (txmobco>=0.5 or nbmob>=10) then tmob=3;

		if LRS NE "" AND RS NE "" then do; 
			if tmob>=3 and RS=LRS then tmob=1;
			else if tmob>=3 and (index(compbl(" "!!RS!!" "),compbl(" "!!LRS!!" "))>0 
									or index(compbl(" "!!LRS!!" "),compbl(" "!!RS!!" "))>0)
							OR (SCAN(LRS,3)= SCAN(RS,3) 
										AND SCAN(LRS,2)= SCAN(RS,2) 
										AND SCAN(LRS,1)= SCAN(RS,1)) 
							OR (SCAN(LRS,2)= SCAN(RS,2) 
									AND SCAN(LRS,1)= SCAN(RS,1) 
									AND SCAN(LRS,2) NOT IN 
										("ET",
										"&",
										"AIR",
										"ASCENSEURS",
										"ASSISTANCE",
										"ASSURANCES",
										"AU",
										"B",
										"BANCO",
										"BANQUE",
										"BUREAU",
										"CABINET",
										"CAISSE",
										"CENTRE",
										"CIE",
										"CLINIQUE",
										"CLOISONS",
										"COMPAGNIE",
										"CONSEILLERS",
										"COOP",
										"CREDIT",
										"CTRE",
										"DE",
										"DES",
										"DESSERTS",
										"DU",
										"E",
										"ECOLE",
										"EDITIONS",
										"ENTREPRISE",
										"ETABLISSEMENTS",
										"EURO",
										"FIDUCIAIRE",
										"FINANCE",
										"FINANCIERE",
										"FIRST",
										"FRANCE",
										"GARAGE",
										"GESTION",
										"GIE",
										"GROUPE",
										"HOTEL",
										"HOTELIERE",
										"HR",
										"INST",
										"INSTITUT",
										"JEAN",
										"LA",
										"LABORATOIRE",
										"LABORATOIRES",
										"LASER",
										"LE",
										"LES",
										"METHODES",
										"MIROITERIE",
										"PARIS",
										"PHARMACIE",
										"PHILIPPE",
										"POMPES",
										"PUBLICATIONS",
										"S",
										"SA",
										"SAS",
										"SCA",
										"SNC",
										"SOC",
										"SOCIETE",
										"SPIE",
										"STE",
										"TELEVISION"))
									OR (SCAN(LRS,1)= SCAN(RS,1) AND SCAN(LRS,1) NOT IN 
										("ET",
										"&",
										"AIR",
										"ASCENSEURS",
										"ASSISTANCE",
										"ASSURANCES",
										"AU",
										"B",
										"BANCO",
										"BANQUE",
										"BUREAU",
										"CABINET",
										"CAISSE",
										"CENTRE",
										"CIE",
										"CLINIQUE",
										"CLOISONS",
										"COMPAGNIE",
										"CONSEILLERS",
										"COOP",
										"CREDIT",
										"CTRE",
										"DE",
										"DES",
										"DESSERTS",
										"DU",
										"E",
										"ECOLE",
										"EDITIONS",
										"ENTREPRISE",
										"ETABLISSEMENTS",
										"EURO",
										"FIDUCIAIRE",
										"FINANCE",
										"FINANCIERE",
										"FIRST",
										"FRANCE",
										"GARAGE",
										"GESTION",
										"GIE",
										"GROUPE",
										"HOTEL",
										"HOTELIERE",
										"HR",
										"INST",
										"INSTITUT",
										"JEAN",
										"LA",
										"LABORATOIRE",
										"LABORATOIRES",
										"LASER",
										"LE",
										"LES",
										"METHODES",
										"MIROITERIE",
										"PARIS",
										"PHARMACIE",
										"PHILIPPE",
										"POMPES",
										"PUBLICATIONS",
										"S",
										"SA",
										"SAS",
										"SCA",
										"SNC",
										"SOC",
										"SOCIETE",
										"SPIE",
										"STE",
										"TELEVISION"))
						then tmob=2;
		end;

		if lapet_1 in ("7820","7820Z","745B","7450","745") AND apet in ("7820","7820Z","745B","7450","745") and tmob=5 then tmob=5.1; 
		else if lapet_1 in ("7820","7820Z","745B","7450","745") AND apet in ("7820","7820Z","745B","7450","745") and tmob=3 then tmob=3.3; 
		else if lapet_1 in ("7820","7820Z","745B","7450","745") AND tmob IN (3,5) then tmob=3.1; 
		else if apet in ("7820","7820Z","745B","7450","745") and tmob=3 then tmob=3.2; 
	run;

	proc datasets lib=user;
	 	delete bm5; 
	run; 

	
	proc freq data=bm6; tables tmob dmob; 
	format dmob dmob. tmob tmob.;
	where tmob NE 0; 
	run;

	%if &year=2007 %then %do; 
		data rec.&b.fullmob; set bm6; run;
	%end;

	proc sql; 
		
			CREATE TABLE rec.&b.flows AS SELECT 
			sum(tmob=1) as NBTMOB1,
			sum(tmob=2) as NBTMOB2,
			sum(round(tmob,1)=3) as NBTMOB3,
			sum(tmob=3.1) as NBTMOB31,
			sum(tmob=3.2) as NBTMOB32,
			sum(tmob=3.3) as NBTMOB33,
			sum(tmob=4) as NBTMOB4,
			sum(tmob=4 & ecart>92) as NBTMOB41,
			sum(round(tmob)=5) as NBTMOB5,
			sum(tmob=5.1) as NBTMOB51,
			mean(lend) as mlend,
			mean(start) as mstart,
			min(year) as year,
			min(lind_1) as lind_1,
			min(ind) as ind,
			min(lapet_1) as lapet_1,
			min(apet) as apet,
			min(lnbsiret_1) as lsize_est,
			min(lsiret) as lsiret,
			min(nbsiret) as size_est,
			min(siret) as siret 
			FROM bm6
			GROUP BY siret, lsiret;
		

		CREATE TABLE  rec.&b.departure AS SELECT 
		sum(tmob=1) as NBTMOB1,
		sum(tmob=2) as NBTMOB2,
		sum(round(tmob,1)=3) as NBTMOB3,
		sum(tmob=3.1) as NBTMOB31,
		sum(tmob=3.2) as NBTMOB32,
		sum(tmob=3.3) as NBTMOB33,
		sum(tmob=4) as NBTMOB4,
		sum(tmob=4 & ecart>92) as NBTMOB41,
		sum(round(tmob)=5) as NBTMOB5,
		sum(tmob=5.1) as NBTMOB51,
		min(year) as year,
		min(lnbsiret_1) as lsize_est,
		min(lind_1) as lind_1,
		min(lapet_1) as lapet_1,
		min(lsiret) as lsiret
		FROM bm6
		GROUP BY lsiret;

		CREATE TABLE  rec.&b.arrival AS SELECT 
		sum(tmob=1) as NBTMOB1,
		sum(tmob=2) as NBTMOB2,
		sum(round(tmob,1)=3) as NBTMOB3,
		sum(tmob=3.1) as NBTMOB31,
		sum(tmob=3.2) as NBTMOB32,
		sum(tmob=3.3) as NBTMOB33,
		sum(tmob=4) as NBTMOB4,
		sum(tmob=4 & ecart>92) as NBTMOB41,
		sum(round(tmob)=5) as NBTMOB5,
		sum(tmob=5.1) as NBTMOB51,
		min(year) as year,
		min(nbsiret) as size_est,
		min(ind) as ind,
		min(apet) as apet,
		min(siret) as siret
		from bm6
		group by siret;

	create table rec.&b.nm_dep_arr as select
		min(siret) as est,
		min(ind) as ind,
		min(apet) as apet,
		min(year) as year,
		min(nbsiret_1) as lsize_est,
		min(nbsiret) as size_est,
		sum(start<=1) as NBDEFDEP,
		sum(end>=720) as NBDEFARR
		from bm3_bis
		group by siret;
	quit;

	quit;
	proc datasets library=USER; 
		DELETE &b  bm bm0  bm1_drop nm tm bm6 bm3_bis ;
	run;
%mend;

%macro common;
	if year<=2001 then do; 
		siren=compress(substr(siren,2,9));
		nic=compress(substr(nic,2,5));
	end;
	siret=compress(""!!compress(siren!!nic)!!"");
%mend;

* DATASET SELECTION ; 

		%macro b_b(year);
			%let b_b_rename=();
			%let b_b_keep=SIREN NIC IDENT_S  REGT_1 REGT
						DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD 
						EFF_MOY_ET_1 EFF_MOY_ET TYP_EMPLOI_1 TYP_EMPLOI APET APET_1;
			%let b_b_where=AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			%let year2=%substr(&year,3,2);
			data b&year; 
			set 
			po&year..post11&year2 (keep=&b_b_keep WHERE=((REGT_1="11" OR REGT="11") &b_b_where))
			po&year..post21&year2 (keep=&b_b_keep WHERE=((REGT_1="21" OR REGT="21") &b_b_where)) 
			po&year..post22&year2 (keep=&b_b_keep WHERE=((REGT_1="22" OR REGT="22") &b_b_where)) 
			po&year..post23&year2 (keep=&b_b_keep WHERE=((REGT_1="23" OR REGT="23") &b_b_where)) 
			po&year..post24&year2 (keep=&b_b_keep WHERE=((REGT_1="24" OR REGT="24") &b_b_where)) 
			po&year..post25&year2 (keep=&b_b_keep WHERE=((REGT_1="25" OR REGT="25") &b_b_where)) 
			po&year..post26&year2 (keep=&b_b_keep WHERE=((REGT_1="26" OR REGT="26") &b_b_where)) 
			po&year..post31&year2 (keep=&b_b_keep WHERE=((REGT_1="31" OR REGT="31") &b_b_where)) 
			po&year..post41&year2 (keep=&b_b_keep WHERE=((REGT_1="41" OR REGT="41") &b_b_where)) 
			po&year..post42&year2 (keep=&b_b_keep WHERE=((REGT_1="42" OR REGT="42") &b_b_where)) 
			po&year..post43&year2 (keep=&b_b_keep WHERE=((REGT_1="43" OR REGT="43") &b_b_where)) 
			po&year..post52&year2 (keep=&b_b_keep WHERE=((REGT_1="52" OR REGT="52") &b_b_where)) 
			po&year..post53&year2 (keep=&b_b_keep WHERE=((REGT_1="53" OR REGT="53") &b_b_where)) 
			po&year..post54&year2 (keep=&b_b_keep WHERE=((REGT_1="54" OR REGT="54") &b_b_where)) 
			po&year..post72&year2 (keep=&b_b_keep WHERE=((REGT_1="72" OR REGT="72") &b_b_where)) 
			po&year..post73&year2 (keep=&b_b_keep WHERE=((REGT_1="73" OR REGT="73") &b_b_where)) 
			po&year..post74&year2 (keep=&b_b_keep WHERE=((REGT_1="74" OR REGT="74") &b_b_where)) 
			po&year..post82&year2 (keep=&b_b_keep WHERE=((REGT_1="82" OR REGT="82") &b_b_where)) 
			po&year..post83&year2 (keep=&b_b_keep WHERE=((REGT_1="83" OR REGT="83") &b_b_where)) 
			po&year..post91&year2 (keep=&b_b_keep WHERE=((REGT_1="91" OR REGT="91") &b_b_where))
			po&year..post93&year2 (keep=&b_b_keep WHERE=((REGT_1="93" OR REGT="93") &b_b_where))
			po&year..post94&year2 (keep=&b_b_keep WHERE=((REGT_1="94" OR REGT="94") &b_b_where))
			;
			year=&year;
			 %common;
			drop TYP_EMPLOI;
			run;
		%mend;




		%macro b_c(year);
			%let b_c_rename=();
			%let b_c_keep=SIREN NIC IDENT_S  REGT REGT_1 
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL /*DOMEMPL_EMPL_1*/ APET APET_1;
			%let b_c_where=AND DOMEMPL_EMPL not in ("1","7") /*AND DOMEMPL_EMPL_1 not in ("1","7")*/ 
							AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			data b&year; 
			set 
			po&year..post11aa (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where )) 
			po&year..post11bb (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where )) 
			po&year..post11cc (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where )) 

			po&year..post11dd (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where )) 
			po&year..post21 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="21" OR REGT="21") &b_c_where )) 
			po&year..post22 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="22" OR REGT="22") &b_c_where )) 
			po&year..post23 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="23" OR REGT="23") &b_c_where )) 
			po&year..post24 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="24" OR REGT="24") &b_c_where )) 
			po&year..post25 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="25" OR REGT="25") &b_c_where )) 
			po&year..post26 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="26" OR REGT="26") &b_c_where )) 
			po&year..post31 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="31" OR REGT="31") &b_c_where )) 
			po&year..post41 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="41" OR REGT="41") &b_c_where )) 
			po&year..post42 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="42" OR REGT="42") &b_c_where )) 
			po&year..post43 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="43" OR REGT="43") &b_c_where )) 
			po&year..post52 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="52" OR REGT="52") &b_c_where )) 
			po&year..post53 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="53" OR REGT="53") &b_c_where )) 
			po&year..post54 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="54" OR REGT="54") &b_c_where )) 
			po&year..post72 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="72" OR REGT="72") &b_c_where )) 
			po&year..post73 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="73" OR REGT="73") &b_c_where )) 
			po&year..post74 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="74" OR REGT="74") &b_c_where )) 
			po&year..post82 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="82" OR REGT="82") &b_c_where )) 
			po&year..post83 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="83" OR REGT="83") &b_c_where )) 
			po&year..post91 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="91" OR REGT="91") &b_c_where )) 
			po&year..post93 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="93" OR REGT="93") &b_c_where ))
			po&year..post94 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="94" OR REGT="94") &b_c_where )) 
			;
			year=&year;
			CS=SUBSTR(CS,1,2);
			%common;
			drop TYP_EMPLOI DOMEMPL_EMPL;
			run;
		%mend;


		%macro b_d(year);
			%let b_c_rename=();
			%let b_c_keep=SIREN NIC IDENT_S  REGT REGT_1 
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL DOMEMPL_EMPL_1 APET APET_1;
			%let b_c_where=AND DOMEMPL_EMPL not in ("1","7") AND DOMEMPL_EMPL_1 not in ("1","7") 
								AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			data b&year; 
			set 
			po&year..post11aa (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where ))
			po&year..post11bb (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where ))
			po&year..post11cc (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where ))
			po&year..post11dd (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where ))
			po&year..post21 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="21" OR REGT="21") &b_c_where )) 
			po&year..post22 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="22" OR REGT="22") &b_c_where )) 
			po&year..post23 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="23" OR REGT="23") &b_c_where )) 
			po&year..post24 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="24" OR REGT="24") &b_c_where )) 
			po&year..post25 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="25" OR REGT="25") &b_c_where )) 
			po&year..post26 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="26" OR REGT="26") &b_c_where )) 
			po&year..post31 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="31" OR REGT="31") &b_c_where )) 
			po&year..post41 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="41" OR REGT="41") &b_c_where )) 
			po&year..post42 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="42" OR REGT="42") &b_c_where )) 
			po&year..post43 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="43" OR REGT="43") &b_c_where )) 
			po&year..post52 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="52" OR REGT="52") &b_c_where )) 
			po&year..post53 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="53" OR REGT="53") &b_c_where )) 
			po&year..post54 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="54" OR REGT="54") &b_c_where )) 
			po&year..post72 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="72" OR REGT="72") &b_c_where )) 
			po&year..post73 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="73" OR REGT="73") &b_c_where )) 
			po&year..post74 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="74" OR REGT="74") &b_c_where )) 
			po&year..post82a (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="82" OR REGT="82") &b_c_where )) 
			po&year..post82b (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="82" OR REGT="82") &b_c_where )) 
			po&year..post83 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="83" OR REGT="83") &b_c_where )) 
			po&year..post91 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="91" OR REGT="91") &b_c_where )) 
			po&year..post93 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="93" OR REGT="93") &b_c_where )) 
			po&year..post94 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="94" OR REGT="94") &b_c_where )) 
			;
			year=&year;
			CS=SUBSTR(CS,1,2);
			%common;
			drop TYP_EMPLOI DOMEMPL_EMPL;
			run;
		%mend;


		%macro b_e(year);
			
			%let b_c_rename=();
			%let b_c_keep=SIREN NIC IDENT_S  REGT REGT_1 
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL DOMEMPL_EMPL_1 APET APET_1;
			%let b_c_where=AND DOMEMPL_EMPL not in ("1","7") AND DOMEMPL_EMPL_1 not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			data b&year; 
				set 
				po&year..post11 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="11" OR REGT="11") &b_c_where ))
				po&year..post21 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="21" OR REGT="21") &b_c_where )) 
				po&year..post22 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="22" OR REGT="22") &b_c_where )) 
				po&year..post23 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="23" OR REGT="23") &b_c_where )) 
				po&year..post24 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="24" OR REGT="24") &b_c_where )) 
				po&year..post25 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="25" OR REGT="25") &b_c_where )) 
				po&year..post26 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="26" OR REGT="26") &b_c_where )) 
				po&year..post31 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="31" OR REGT="31") &b_c_where )) 
				po&year..post41 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="41" OR REGT="41") &b_c_where )) 
				po&year..post42 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="42" OR REGT="42") &b_c_where )) 
				po&year..post43 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="43" OR REGT="43") &b_c_where )) 
				po&year..post52 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="52" OR REGT="52") &b_c_where )) 
				po&year..post53 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="53" OR REGT="53") &b_c_where )) 
				po&year..post54 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="54" OR REGT="54") &b_c_where )) 
				po&year..post72 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="72" OR REGT="72") &b_c_where )) 
				po&year..post73 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="73" OR REGT="73") &b_c_where )) 
				po&year..post74 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="74" OR REGT="74") &b_c_where )) 
				po&year..post82 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="82" OR REGT="82") &b_c_where )) 
				po&year..post83 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="83" OR REGT="83") &b_c_where )) 
				po&year..post91 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="91" OR REGT="91") &b_c_where )) 
				po&year..post93 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="93" OR REGT="93") &b_c_where )) 
				po&year..post94 (RENAME=&b_c_rename keep=&b_c_keep WHERE=((REGT_1="94" OR REGT="94") &b_c_where )) 
				;
				year=&year;
				CS=SUBSTR(CS,1,2);
				%common;
				drop TYP_EMPLOI DOMEMPL_EMPL;
			run;
		%mend;

		%macro b_f(year);
			%let b_f_rename=();
			%let b_f_keep=SIREN NIC IDENT_S  REGT REGT_1 DEPT_1 DEPT
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL DOMEMPL_EMPL_1 APET APET_1;
			%let b_f_where=AND DOMEMPL_EMPL not in ("1","7") AND DOMEMPL_EMPL_1 not in ("1","7") 
							AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			data b&year; 
				set 
				pi&year..post75 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="75" OR DEPT="75") &b_f_where ))
				pi&year..post77 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="77" OR DEPT="77") &b_f_where ))
				pi&year..post78 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="78" OR DEPT="78") &b_f_where ))
				pi&year..post91 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="91" OR DEPT="91") &b_f_where ))
				pi&year..post92 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="92" OR DEPT="92") &b_f_where ))
				pi&year..post93 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="93" OR DEPT="93") &b_f_where ))
				pi&year..post94 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="94" OR DEPT="94") &b_f_where ))
				pi&year..post95 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="95" OR DEPT="95") &b_f_where ))

				po&year..post22 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="22" OR REGT="22") &b_f_where )) 
				po&year..post23 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="23" OR REGT="23") &b_f_where )) 
				po&year..post24 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="24" OR REGT="24") &b_f_where )) 
				po&year..post25 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="25" OR REGT="25") &b_f_where )) 
				po&year..post26 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="26" OR REGT="26") &b_f_where )) 
				po&year..post31 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="31" OR REGT="31") &b_f_where )) 
				po&year..post41 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="41" OR REGT="41") &b_f_where )) 
				po&year..post42 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="42" OR REGT="42") &b_f_where )) 
				po&year..post43 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="43" OR REGT="43") &b_f_where )) 
				po&year..post52 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="52" OR REGT="52") &b_f_where )) 
				po&year..post53 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="53" OR REGT="53") &b_f_where )) 
				po&year..post54 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="54" OR REGT="54") &b_f_where )) 
				po&year..post72 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="72" OR REGT="72") &b_f_where )) 
				po&year..post73 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="73" OR REGT="73") &b_f_where )) 
				po&year..post74 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="74" OR REGT="74") &b_f_where )) 
				po&year..post82 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="82" OR REGT="82") &b_f_where )) 
				po&year..post83 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="83" OR REGT="83") &b_f_where )) 
				po&year..post91 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="91" OR REGT="91") &b_f_where )) 
				po&year..post93 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="93" OR REGT="93") &b_f_where )) 
				po&year..post94 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="94" OR REGT="94") &b_f_where )) 
				;
				year=&year;
				CS=SUBSTR(CS,1,2);
				%common;
				drop TYP_EMPLOI DOMEMPL_EMPL;
			run;
		%mend;


		%macro b_g(year);
			%let b_f_rename=();
			%let b_f_keep=SIREN NIC IDENT_S  REGT REGT_1 DEPT_1 DEPT
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL DOMEMPL_EMPL_1 APET APET_1;
			%let b_f_where=AND DOMEMPL_EMPL not in ("1","7") AND DOMEMPL_EMPL_1 not in ("1","7") 
							AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;

			data b&year; 
				set 
				pi&year..post75 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="75" OR DEPT="75") &b_f_where ))
				pi&year..post77 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="77" OR DEPT="77") &b_f_where ))
				pi&year..post78 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="78" OR DEPT="78") &b_f_where ))
				pi&year..post91 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="91" OR DEPT="91") &b_f_where ))
				pi&year..post92 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="92" OR DEPT="92") &b_f_where ))
				pi&year..post93 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="93" OR DEPT="93") &b_f_where ))
				pi&year..post94 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="94" OR DEPT="94") &b_f_where ))
				pi&year..post95 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((DEPT_1="95" OR DEPT="95") &b_f_where ))

				po&year..post24 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="24" OR REGT="24") &b_f_where )) 
				po&year..post27 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="27" OR REGT="27") &b_f_where )) 
				po&year..post28 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="28" OR REGT="28") &b_f_where )) 
				po&year..post32 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="32" OR REGT="32") &b_f_where )) 
				po&year..post44 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="44" OR REGT="44") &b_f_where )) 
				po&year..post52 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="52" OR REGT="52") &b_f_where )) 
				po&year..post53 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="53" OR REGT="53") &b_f_where )) 
				po&year..post75 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="75" OR REGT="75") &b_f_where )) 
				po&year..post76 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="76" OR REGT="76") &b_f_where )) 
				po&year..post84 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="84" OR REGT="84") &b_f_where )) 
				po&year..post93 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="93" OR REGT="93") &b_f_where ))
				po&year..post94 (RENAME=&b_f_rename keep=&b_f_keep WHERE=((REGT_1="94" OR REGT="94") &b_f_where )) 
				;
				year=&year;
				CS=SUBSTR(CS,1,2);
				%common;
				drop TYP_EMPLOI DOMEMPL_EMPL;
				run;
		%mend;
			
		%macro b_h(year);
			%let keep=SIREN NIC IDENT_S  REGT REGT_1 DEPT_1
							DATDEB_1 DATFIN_1 DATDEB DATFIN CPFD_1 CPFD  
							TYP_EMPLOI TYP_EMPLOI_1 DOMEMPL_EMPL DOMEMPL_EMPL_1 APET APET_1;
			%let where=DOMEMPL_EMPL not in ("1","7") AND DOMEMPL_EMPL_1 not in ("1","7") 
						AND TYP_EMPLOI NOT IN ("E","I") AND TYP_EMPLOI_1 NOT IN ("E","I") ;
			%let rename=;


			data b&year; 
				set 
				po&year..post_1 (&rename KEEP=&keep WHERE=(&where))
				po&year..post_2 (&rename KEEP=&keep WHERE=(&where))
				po&year..post_3 (&rename KEEP=&keep WHERE=(&where))
				po&year..post_4 (&rename KEEP=&keep WHERE=(&where))
				;
				year=&year;
					%common;		 
				CS=SUBSTR(CS,1,2);
				drop TYP_EMPLOI DOMEMPL_EMPL;
			run;
		%mend;

%b_b(2002);%out(b2002);
%b_b(2003);%out(b2003);
%b_b(2004);%out(b2004);
%b_b(2005);%out(b2005);
%b_b(2006);%out(b2006);
%b_b(2007);%out(b2007);
%b_b(2008);%out(b2008);
/*%b_c_1(2009);%out(b2009_1);*/
%b_c(2009);%out(b2009);
%b_d(2010);%out(b2010);
%b_f(2012);%out(b2012);
%b_e(2011);%out(b2011);
%b_f(2013);%out(b2013);
%b_g(2014);%out(b2014);
%b_g(2015);%out(b2015);
%b_g(2016);%out(b2016);
%b_g(2017);%out(b2017);
%b_h(2018);%out(b2018);
%b_h(2019);%out(b2019);



proc means data=b2004movers;var txmobco nbmob; 
where tmob=1; 
run;

proc means data=b2004movers;var txmobco nbmob; 
where tmob=1 and txmobco2>0.5; 
run;

proc print data=b2004movers (obs=20);var siren lnic nic nbmob lnbsiret_1 nbsiret lstart lend start end ; 
where tmob=1 and txmobco2>0.8 and nbmob>=5; 
run;


proc freq data=b2004movers;tables tmob; 
format tmob tmob.; 
where nbmob>10;
run;

proc freq data=b2004_4;tables type_m; 
format type_m type_m.; 
run;

proc sort data=b2004_4; by ident_s start;run;

proc means data=bm6; var ecart; run;
